const app = getApp()
const util = require('../../../utils/util.js')

// 返回处理
const handleRes = (response, fn, url, data, confirmFn) => {
  console.log(url, 'res-->>', response)
  if (response.statusCode == 200) {
    if (response.data.result == 100000) {
      wx.reLaunch({
        url: '../index/index',
      })
      return
    } else if ((response.data.result == 0 || response.data.result === undefined) && typeof fn == 'function') {
      fn(response.data)
      return;
    } else if (response.data.result == 1001) {
      wx.showModal({
        title: '温馨提示',
        confirmColor: '#8290DD',
        content: response.data.description,
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定');
            if (confirmFn) {
              confirmFn(true);
            }
          } else if (res.cancel) {
            console.log('用户点击取消');
          }
        }
      })
      return;
    } else {
      wx.showModal({
        title: '温馨提示',
        confirmColor: '#8290DD',
        showCancel: false,
        content: response.data.description
      })
    }
  } else {
    // app.sendLog(url, JSON.stringify(response), JSON.stringify(data));
    wx.showToast({
      title: '服务' + response.statusCode,
      image: '/images/gantan.png',
      duration: 2500
    })
    return;
  }
}

const handleErr = (error, url, data) => {
  console.log(url, 'error-->>', error)
  wx.showToast({
    title: '请求失败请重试',
    image: '/images/gantan.png',
    duration: 2500
  })
}

const handleRequest = (data, fn, url, confirmFn) => {

  handleRequestCallback(data, fn, url, confirmFn)

}

const handleRequestCallback = (data, fn, url, confirmFn) => {
  wx.showLoading({
    title: '加载中...',
    mask: true
  })

  wx.request({
    url: util.CONST_DATA.BASE_URL + url,
    header: header,
    method: method,
    data: data,
    success: function (response) {
      wx.hideLoading()
      handleRes(response, fn, url, data, confirmFn)
    },
    fail: function (error) {
      wx.hideLoading()
      handleErr(error, url, data)
    }
  })
}


const header = app.globalData.header
const method = 'POST'

const lbbTaskService = {

  /**
   * 获取基本配置信息
   */
  getTaskList: (data, fn) => {
    let url = 'photo/taskList'
    handleRequest(data, fn, url)
  },

  /**
   * 设置任务的时间周期
   * @param {*} data 
   * @param {*} fn 
   */
  setTaskTimeConf:(data, fn) =>{
    let url = 'photo/setTimeConf'
    handleRequest(data, fn, url)
  },

  /**
   * 获取拍照时间配置列表
   * @param {*} data 
   * @param {*} fn 
   */ 
  getTaskTimeConf:(data, fn) =>{
    let url = 'photo/taskTimeConfList';
    handleRequest(data, fn, url)
  },

  /**
   * 根据id获取拍照配置信息
   * @param {*} data 
   * @param {*} fn 
   */
  getTaskTimeConfById:(data, fn)=>{
     let url = 'photo/getTaskTimeConfById'
     handleRequest(data, fn, url)
  },
  
  /**
   * 删除拍照任务时间配置
   * @param {*} data 
   * @param {*} fn 
   */
  delTaskTimeConfById:(data, fn)=>{
    let url = 'photo/delTimeConf'
    handleRequest(data, fn, url)
  },

  /**
   * 获取拍照医院统计信息
   * @param {*} data 
   * @param {*} fn 
   */
  getPhotoHosStatis:(data, fn)=>{
    let url = 'photo/hosList'
    handleRequest(data, fn, url)
  },

  /**
   * 获取巡检工作人员列表
   * @param {*} data 
   * @param {*} fn 
   */
  getRoutingWorkerList:(data, fn)=>{
    let url = 'user/routingWorker'
    handleRequest(data, fn, url)
  },

  /**
   * 拍照任务审查结果上报
   * @param {*} data 
   * @param {*} fun 
   */
  photoCheckRst:(data, fun)=>{
     let url = 'photo/check';
     handleRequest(data, fun, url)
  },
  
  /**
   * 获取拍照的设备列表
   * @param {*} data 
   * @param {*} fun 
   */
  getPhotoEquList:(data, fun) =>{
    let url = 'photo/equList'
    handleRequest(data, fun, url)
  },

  /**
   * 获取设备的拍照文件信息
   * @param {*} data 
   * @param {*} fun 
   */
  getEquPhotoFiles:(data, fun) =>{
    let url = 'photo/equPhotoList'
    handleRequest(data, fun, url)
  },

  /**
   * 获取设备照片上传的文件列表
   * @param {*} data 
   * @param {*} fun 
   */
  getEquPhotoUploadFiles:(data, fun) =>{
    let url = 'photo/equUploadPhotoList'
    handleRequest(data, fun, url)
  },

  /**
   * 上传照片信息
   * @param {*} data 
   * @param {*} fun 
   */
  uploadPhotoFile:(data, fun) => {
    console.log("upload--->", data)
    let url = util.CONST_DATA.BASE_URL + 'photo/uploadPhoto'
    wx.uploadFile({
      header,
      filePath: data.url,
      name: 'file',
      url: url,
      formData:{
        fileType: data.type,
        fileSize: data.size,
        fileName: data.name,
        equRecordId: data.equRecordId
      },
      success(res) {
         console.log("photoUploadImg --->", res)
         fun(JSON.parse(res.data))
      }

    })
  },

  /**
   * 提交上传的文件
   * @param {*} data 
   * @param {*} fun 
   */
  commitUploadFile:(data, fun) =>{
     let url = 'photo/commitPhotoFile';
     handleRequest(data, fun, url)
  }

}
 

module.exports = lbbTaskService